package com.lzit.smart_canteen.service;

import com.lzit.smart_canteen.domain.Authority;
import com.lzit.smart_canteen.domain.User;
import com.lzit.smart_canteen.domain.UserAuthority;
import com.lzit.smart_canteen.repository.AuthorityRepository;
import com.lzit.smart_canteen.repository.UserAuthorityRepository;
import com.lzit.smart_canteen.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private AuthorityRepository authorityRepository;
    @Autowired
    private UserAuthorityRepository userAuthorityRepository;
    //注册
    public String register(User user) {
        User user1 = userRepository.findUserByNum(user.getNum());
        if (user1!=null) {
            return "EXIST";
        } else {
            userRepository.save(user);
            User user2 = userRepository.findUserByNum(user.getNum());
            UserAuthority userAuthority = new UserAuthority();
            userAuthority.setUser_id(user2.getId());
            userAuthority.setAuthority_id(user2.getValid());
            System.out.println(userAuthority.toString());
            userAuthorityRepository.save(userAuthority);

            return "SUCCESS";
        }
    }
    //登录
    public String loginIn(User user) {
        User user1 = userRepository.findUserByNum(user.getNum());
        if (user1 != null){
            if(!Objects.equals(user1.getPassword(), user.getPassword())){
                return "FALSE";
            } else {
                return "TRUE";
            }
        } else {
            return "NULL";
        }
    }
    //添加
    public void addUser(User user) {
        userRepository.save(user);
    }
    //删除
    public void removeUser() {

    }
    //查询单个
    public User findUser() {
        return null;
    }
    //查询所有
    public List<User> findAll() {
        return userRepository.findAll();
    }
    public List<User> findByName(String name) {
        return userRepository.findUsersByName(name);
    }
    public User findById(Integer id) {
        return userRepository.findUserById(id);
    }
    public User findByNum(String num) {
        return userRepository.findUserByNum(num);
    }
    public List<User> findByValid(int valid) {
        return userRepository.findUserByValid(valid);
    }
    //修改
}
